import java.util.ArrayList;
import java.util.StringTokenizer;


public class CodeSegmentation
{
	private ArrayList<CodeLine> codelines;
	private LabelDivision labelDivision = new LabelDivision();
	private BlankProcess blankProcess = new BlankProcess();
	private CommentProcess commentProcess = new CommentProcess();
	
	
	public CodeSegmentation(Code codes)
	{
		codelines = new ArrayList<CodeLine>(); 
		segmentation(codes);

	}
	
	private void segmentation(Code codes)
	{
		
		String commandString = codes.getInitialCode();
  	StringTokenizer st = new StringTokenizer(commandString,"\n");  //split the command using delimeter CR ;
    ArrayList<String> strsplit = new ArrayList<String>();  // Arraylist strsplit is used to store the purely command in order.
    String stringRecord,stringRecord2;
    int i=0,countLine=0;// countLine is a counter of the line number
    CodeLine codeLine;
    
    while(st.hasMoreElements()){ 	 
    	 codeLine = new CodeLine();
       stringRecord=st.nextElement().toString();
       int a=stringRecord.trim().length();// here int a is used to check wether the line is a blank line. if it is, a will be 0.
       codeLine.setLineNum(countLine+1);
       countLine++;
 
       if(a!=0){                   // if the line is not a blank line, there must be some commands or comments there.          
          stringRecord2=commentProcess.cProcess(stringRecord,codeLine);
          labelDivision.lDivision(stringRecord2,codeLine);
          blankProcess.bProcess(codeLine);
       }
       codelines.add(codeLine);

   } 
	}	
	
	public ArrayList getCodeLines()
	{
		return codelines;
	}

}